package com.ruoyi.demo.service.impl;

import com.ruoyi.system.domain.*;
import com.ruoyi.demo.domain.vo.meetLogVO;
import com.ruoyi.demo.service.IManagerMeetLogService;
import com.ruoyi.system.domain.vo.MeetUserAttented;
import com.ruoyi.system.mapper.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 33692
 */
@Service
public class ManagerMeetLogServicelmpl implements IManagerMeetLogService {

    @Resource
    private com.ruoyi.system.mapper.MeetLogMapper meetLogMapper;

    @Resource
    MeetMapper meetMapper;

    @Resource
    RoomMapper roomMapper;

    @Resource
    SysUserMapper userMapper;

    @Resource
    MeetMaterialsMapper meetMaterialsMapper;

    @Resource
    AgendaMapper agendaMapper;


    @Override
    public List<meetLogVO> selectMeetLogList(MeetLog meetLog) {
        // 添加详细的调试日志
        System.out.println("接收到的查询参数:");
        System.out.println("- 主题: " + meetLog.getTheme());
        System.out.println("- 状态: " + meetLog.getStatus());
        System.out.println("- 参会人员: " + meetLog.getAttendUser());
        if (meetLog.getParams() != null) {
            System.out.println("- 开始时间: " + meetLog.getParams().get("beginStartTime"));
            System.out.println("- 结束时间: " + meetLog.getParams().get("endStartTime"));
        }

        // 确保状态值为字符串类型
        if (meetLog.getStatus() != null) {
            meetLog.setStatus(meetLog.getStatus().toString());
        }

        // 1. 获取会议日志列表
        List<MeetLog> meetLogList = meetLogMapper.selectMeetLogList(meetLog);
        System.out.println("查询到的记录数: " + meetLogList.size());

        if(meetLogList.isEmpty()) {
            return new ArrayList<>();
        }

        // 2. 收集所有需要查询的meetId
        List<Long> meetIds = meetLogList.stream()
                .map(MeetLog::getMeetId)
                .distinct()  // 确保ID不重复
                .collect(Collectors.toList());

        // 3. 批量查询相关数据
        Map<Long, Meet> meetMap = meetMapper.selectMeetByMeetIds(meetIds).stream()
                .collect(Collectors.toMap(
                    meet -> Long.valueOf(meet.getMeetId()), 
                    meet -> meet,
                    (existing, replacement) -> existing  // 处理重复键
                ));

        Map<Long, Room> roomMap = roomMapper.selectRoomByMeetIds(meetIds).stream()
                .collect(Collectors.toMap(Room::getRoomId, room -> room));

        Map<Long, MeetMaterials> materialsMap = meetMaterialsMapper.selectMeetMaterialsByMeetIds(meetIds).stream()
                .collect(Collectors.toMap(MeetMaterials::getId, materials -> materials));

        Map<Long, List<Agenda>> agendaMap = agendaMapper.selectAgendaByMeetIds(meetIds).stream()
                .collect(Collectors.groupingBy(Agenda::getMeetId));

        // 修改这里的分组逻辑，使用 meetId 作为分组键
        List<MeetUserAttented> userList = userMapper.selectAttentedUsersByMeetIds(meetIds);
        Map<Long, List<MeetUserAttented>> userMap = new HashMap<>();
        for (MeetUserAttented user : userList) {
            userMap.computeIfAbsent(user.getMeetId(), k -> new ArrayList<>()).add(user);
        }

        // 4. 组装数据并返回
        return meetLogList.stream()
                .map(log -> {
                    meetLogVO vo = new meetLogVO();
                    Long meetId = log.getMeetId();
                    Meet meet = meetMap.get(meetId);
                    
                    vo.setMeet(meet);
                    if(meet != null) {
                        vo.setRoom(roomMap.get(Long.valueOf(meet.getRoomId())));
                    }
                    vo.setMeetMaterials(materialsMap.get(meetId));
                    vo.setAgendaList(agendaMap.getOrDefault(meetId, new ArrayList<>()));
                    vo.setUserList(userMap.getOrDefault(meetId, new ArrayList<>()));

                    return vo;
                })
                .collect(Collectors.toList());
    }

    @Override
    public void insertMeetLog(Long meetId, Long userId, String attendUser) {
        Meet meet = meetMapper.selectMeetByMeetId(Math.toIntExact(meetId));

        MeetLog meetLog = new MeetLog();
        meetLog.setMeetId(meetId);
        meetLog.setTheme(meet.getTheme());
        meetLog.setMeetContent(meet.getMeetContent());
        meetLog.setStartTime(meet.getStartTime());
        meetLog.setEndTime(meet.getEndTime());
        meetLog.setStatus(meet.getStatus());
        meetLog.setUserId(userId);
        meetLog.setAttendUser(attendUser);

        meetLogMapper.insertMeetLog(meetLog);
    }
}
